package com.hjl.mybatis.utils;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ServivE
 * @version 0.1
 * @className JsonUtil
 * @description
 * @date 2024/8/1 15:21
 * @since jdk11
 */

/**
 * json转换工具类
 */
public class JsonUtil {


    public static void getJson(HttpServletRequest request, HttpServletResponse response, Object object) {
        response.setContentType("text/html;charset=UTF-8");

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -10);

        PrintWriter out = null;
        try {
            out = response.getWriter();
            String jsonStr = JSON.toJSONString(object);
            out.print(jsonStr);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
}
